var JSGF = new Object();

/////////////////////////////////////////
//Private
////////////////////////////////////////

JSGF.__version__ = "0.01";

JSGF.__private__ = new Object();
JSGF.__registry__ = new Object();

JSGF.__private__.getFunctionParameterString = function(arg, start) {
	if (start == null) {
		start = 0;
	}
	var param = "";
	for(var i = start; i<arg.length; i++){
		if (i != start) {
			param += ",";
		}
		param += "arguments[" + i + "]";
	}
	return param;
}

JSGF.__private__.setRegistry = function(k, v) {
	JSGF.__registry__[k] = v;
}

JSGF.__private__.getRegistry = function(k) {
	var v = null;
	if (JSGF.__registry__[k]) {
		v = JSGF.__registry__[k];
	}
	return v;
}

///////////////////////////////////////
// Public
////////////////////////////////////////

JSGF.getVersion = function() {
	return JSGF.__version__;
}

JSGF.loadController = function(name, element) {
	if (typeof(name) != "string") {
		return false;
	}
	var listController = JSGF.__private__.getRegistry("LOAD_CONTROLLER");
	if (listController == null) {
		listController = new Object();
	}
	eval("var controller = new " + name + "(element);");
	if (!controller.type || controller.type !="JSGF.Controller") {
		return false;
	}
	listController[name] = controller;
	JSGF.__private__.setRegistry("LOAD_CONTROLLER", listController);
	return true;
}

JSGF.unloadController = function(name) {
	if (typeof(name) != "string") {
		return false;
	}
	var listController = JSGF.__private__.getRegistry("LOAD_CONTROLLER");
	if (listController == null) {
		return false;
	}
	if (!listController[name]) {
		return false;
	}
	var controller = listController[name];
	if (controller.type != "JSGF.Controller") {
		return false;
	}
	JSGF.release(controller);
	delete listController[name];
	JSGF.__private__.setRegistry("LOAD_CONTROLLER", listController);
	return true;
}

JSGF.getType = function(jsgfObject) {
	if (jsgfObject.type) {
		return jsgfObject.type;
	} else {
		return typeof(jsgfObject);
	}
}

JSGF.inherit = function(jsgfClass) {
	if (!jsgfClass) {
		return null;
	}
	var returnClass = function() {
		if (arguments.length == 0) {
			jsgfClass.call(this);
		} else {
			var param = JSGF.__private__.getFunctionParameterString(arguments);
			eval("jsgfClass.call(this," + param + ");");
		}
	}
	returnClass.prototype = new jsgfClass("__JSGF_INITIALIZE__");
	return returnClass;
}

JSGF.factory = function(jsgfClass) {
	if (!jsgfClass) {
		return null;
	}
	returnClass = null;
	if (arguments.length == 0) {
		returnClass = new jsgfClass();
	} else {
		var param = JSGF.__private__.getFunctionParameterString(arguments, 1);
		eval("returnClass = new jsgfClass(" + param + ");");
	}
	return returnClass;
}

JSGF.release = function(jsgfClass){
	if (jsgfClass.__jsgf__) {
		jsgfClass.release();
		jsgfClass = null;
		return true;
	} else {
		return false;
	}
}

